package com.fenghongzhang.zhoukao2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements OneFragment.MyListener {
    private ViewPager vp;
    private TabLayout tbl;
    private RadioGroup group;


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){


        }else{

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
        }

        tbl = (TabLayout) findViewById(R.id.tbl);

        group = (RadioGroup) findViewById(R.id.group);

        vp = (ViewPager) findViewById(R.id.vp);

        List<Fragment> list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        list.add(new ThreeFragment());

        //biaoti
        List<String> title = new ArrayList<>();
        title.add("hehe");
        title.add("hehe");
        title.add("hehe");

        MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(),list,title);

        vp.setAdapter(myAdapter);

        tbl.setupWithViewPager(vp);


        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        vp.setCurrentItem(2);
                        break;
                }


            }
        });



    }

    @Override
    public void click(int i,String s) {
        vp.setCurrentItem(i);

        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }
}
